package main

import (
	"bytes"
	"fmt"
	"io/ioutil"
	"net/http"
	"os"
	"os/exec"

	"gitee.com/zjzhang/log"
	"gitee.com/zjzhang/weixin"
)

const (
	mediaUrl = "https://api.weixin.qq.com/cgi-bin/media/get?access_token=%s&media_id=%s"
)
type custMsgText struct {
	Content string `json:"content"` //	文本消息内容
}

func defaultHandler(msg *weixin.Message) weixin.ReplyMsg {
	log.Debugf("%+v", msg)
	SendCustMessge(msg)
	return nil
}

func echoMsgText(m *weixin.RecvText) weixin.ReplyMsg {
	log.Debugf("receive message: %+v", m)
	ret := &weixin.ReplyText{
		ToUserName:   m.FromUserName,
		FromUserName: m.ToUserName,
		CreateTime:   m.CreateTime,
		Content:      m.FromUserName + ", " + m.Content,
	}

	log.Debugf("replay message: %+v", ret)
	return ret
}

func echoMsgImage(m *weixin.RecvImage) weixin.ReplyMsg {
	log.Debugf("%+v", m)
	// echo message
	ret := &weixin.ReplyImage{
		ToUserName:   m.FromUserName,
		FromUserName: m.ToUserName,
		CreateTime:   m.CreateTime,
		PicUrl:       m.PicUrl,
		MediaId:      m.MediaId,
	}

	log.Debugf("%+v", ret)
	return ret
}

var (
	ch = make(chan *weixin.RecvVoice)
)

var client = http.Client{
	Transport: &http.Transport{
		MaxIdleConnsPerHost: 128,
	},
}

func procPlayMedia() {

	for {
		m := <-ch

		url := fmt.Sprintf(mediaUrl, weixin.AccessToken(), m.MediaId)

		log.Debugf("下载语音  %s ", m.MediaId)
		filename, body, _ := weixin.Download(url)
		log.Debugf("识别语音  %s ", filename)

		asrUrl := "http://vop.baidu.com/server_api?dev_pid=1536&cuid=xxxxx&token=25.fb1b1c6b8319235b3c76a9a217be4ae3.315360000.1843623481.282335-10455099"
		resp, err := client.Post(asrUrl, "audio/amr;rate=8000", bytes.NewBuffer(body))
		if err != nil {
			log.Fatal(err)
			continue
		}
		js, _ := ioutil.ReadAll(resp.Body)
		log.Debugf("识别语音结果  %s ", js)
		custMsg := &weixin.CustText{Content: string(js)}
		log.Debugf("发送语音识别内容  %+v", weixin.SendCustomMsg(m.FromUserName, custMsg))

		log.Debugf("保存语音  %s", m.MediaId)
		file, createErr := os.Create(filename)
		if createErr != nil {
			log.Fatal(createErr)
		}

		_, writeErr := file.Write(body)
		if writeErr != nil {
			log.Fatal(writeErr)
		}
		closeErr := file.Close()
		if closeErr != nil {
			log.Fatal(closeErr)
		}
		cmd := exec.Command("omxplayer", filename)
		cmd.Run()
		os.Remove(filename)

	}

}

func echoMsgVoice(m *weixin.RecvVoice) weixin.ReplyMsg {
	log.Debugf("%+v", m)

	// echo message
	//ret := &weixin.ReplyVoice{
	//	ToUserName:   m.FromUserName,
	//	FromUserName: m.ToUserName,
	//	CreateTime:   m.CreateTime,
	//	MediaId:      m.MediaId,
	//}

	ret := &weixin.ReplyText{
		ToUserName:   m.FromUserName,
		FromUserName: m.ToUserName,
		CreateTime:   m.CreateTime,
		Content:      "正在识别...",
	}

	log.Debugf("%+v", ret)
	//获取文件
	go func() {
		ch <- m
	}()

	return ret
}

func echoMsgVideo(m *weixin.RecvVideo) weixin.ReplyMsg {
	log.Debugf("%+v", m)

	// MediaId ???
	ret := &weixin.ReplyVideo{
		ToUserName:   m.FromUserName,
		FromUserName: m.ToUserName,
		CreateTime:   m.CreateTime,
		MediaId:      m.MediaId,
		Title:        "video",
		Description:  "thist is a test desc...",
	}

	log.Debugf("%+v", ret)
	return ret
}

func echoMsgShortVideo(m *weixin.RecvVideo) weixin.ReplyMsg {
	log.Debugf("%+v", m)

	// MediaId ???
	ret := &weixin.ReplyVideo{
		ToUserName:   m.FromUserName,
		FromUserName: m.ToUserName,
		CreateTime:   m.CreateTime,
		MediaId:      m.ThumbMediaId,
		Title:        "shortvideo",
		Description:  "thist is a test desc...",
	}

	log.Debugf("%+v", ret)
	return ret
}

func echoMsgLocation(m *weixin.RecvLocation) weixin.ReplyMsg {
	log.Debugf("%+v", m)

	// echo message
	ret := &weixin.ReplyText{
		ToUserName:   m.FromUserName,
		FromUserName: m.ToUserName,
		CreateTime:   m.CreateTime,
		Content:      weixin.AccessToken(),
	}

	log.Debugf("replay message: %+v", ret)
	return ret
}

func echoMsgLink(m *weixin.RecvLink) weixin.ReplyMsg {
	log.Debugf("%+v", m)

	// 回复图文消息

	return nil
}
